<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\brand\BrandOrder;

$list = [];
//处理品牌订单状态（续费，升级，新开）
$where = [
    'order_status' => BrandOrder::ORDER_STATUS_FINISHED,
    'pay_status' => BrandOrder::PAY_STATUS_PAID,
    'service_status' => BrandOrder::SERVICE_STATUS_FINISHED,
    'ORDER' => ['id' => 'ASC'],
];
$list = BrandOrder::select('*', $where);
$brandOrderList = [];
// 升级：  同品牌&相同入驻时间的订单，更晚支付的为升级类型。
// 新签：  所有品牌首个订单，算为新签。
// 续费：  其他为续费。
foreach ($list as $row) {
    $row['in_time'] = strtotime($row['in_start_time']) . '-' . strtotime($row['in_end_time']);
    $brandOrderList[$row['brand_id']][] = $row;
}
foreach ($brandOrderList as $brandId => $rows) {
    $count = count($rows);
    $inTime = 0;
    $previousInTime = 1;
    for ($i = 0; $i < $count; $i++) {
        $id = $rows[$i]['id'];
        $inTime = $rows[$i]['in_time'];
        if ($i == 0) {
            $orderType = BrandOrder::ORDER_TYPE_NEW;
        } else {
            $orderType = BrandOrder::ORDER_TYPE_RENEW;
            if ($inTime == $previousInTime) {
                $orderType = BrandOrder::ORDER_TYPE_UPGRADE;
            }
        }
        BrandOrder::update(['order_type' => $orderType], ['id' => $id]);
        $previousInTime = $inTime;
    }
}